/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_rtsp_analyze.c                                   */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/05 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../src/module/rtsp/config.h"
#include "../test_rtsp_comm.h"
#include <rtsp/rtsp_core.h>
#include <rtsp/rtsp_header.h>
#include <rtsp/rtsp_comm.h>
#include <rtsp/rtsp_packet.h>
#include <frct_comm.h>
#include <apmtest_comm.h>
#include <frreport_opt.h>
#include <frcomm.h>

extern void __rtsp_add_error_nums(L7session_rtsp *p_st_sess);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    CU_ASSERT_EQUAL(test_rtsp_create_ct(&p_st_conn, TEST_RTSP_SRC_IP, TEST_RTSP_DST_IP, TEST_RTSP_SRC_PORT, TEST_RTSP_DST_PORT), FR_SUCC);

    char *p_payload = (char *) "REDIRECT rtsp://example.com/fizzle/foo RTSP/1.0\r\n"
            "CSeq: 732\r\n"
            "Location: rtsp://bigserver.com:8001\r\n"
            "Range: clock=19960213T143205Z-\r\n\r\n";
    unsigned int ui_payload_len = strlen(p_payload);
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, p_payload, ui_payload_len, TEST_RTSP_SRC_IP, TEST_RTSP_DST_IP, TEST_RTSP_SRC_PORT, TEST_RTSP_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    CU_ASSERT_TRUE(APM_L7_ANALYZE_IS_SUCC(rtsp_analyze(p_st_conn, p_st_packet, &st_hash_report)));

    Frvar_field *p_st_field = NULL;
    unsigned int ui_loop = 0;
    Frlist_header *p_list_report = NULL;
    Frlist_node *p_st_node_report = NULL;
    Frreport_data *p_st_report_data = NULL;
    FRHASH_LOOP(ui_loop, &st_hash_report, p_list_report)
    {
        if (p_list_report == NULL)
            continue;
        p_st_node_report = FRLIST_FIRST_NODE(p_list_report);
        while (p_st_node_report)
        {
            p_st_report_data = (Frreport_data *) FRLIST_GET_NODE_DATA(p_st_node_report);
            if (p_st_report_data)
            {
                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_TRANS_TITLE, 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "rtsp://example.com/fizzle/foo");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_RTSP_REPORT_REQ_METHOD, 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "REDIRECT");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_RTSP_REPORT_REQ_URI, 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "rtsp://example.com/fizzle/foo");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_RTSP_REPORT_REQ_HDR_PREFIX"cseq", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "732");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_RTSP_REPORT_REQ_HDR_PREFIX"location", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "rtsp://bigserver.com:8001");

                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_RTSP_REPORT_REQ_HDR_PREFIX"range", 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "clock=19960213T143205Z-");
            }
            p_st_node_report = FRLIST_NEXT_NODE(p_st_node_report);
        }
    }

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    test_rtsp_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_rtsp_analyze(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" rtsp_analyze()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
